package com.krain.ddbb.activity;


import android.content.Intent;
import android.text.TextUtils;
import android.widget.TextView;

import com.krain.ddbb.R;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.UserAccountBean;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_user_account)
public class UserAccountActivity extends BaseActivity {

    @ViewById(R.id.act_account_tv_money)
    TextView tv_money;
    UserAccountBean bean;

    @ViewById(R.id.bind_card_item)
    TextView mBindCard;
    boolean isBind;

    @AfterViews


    @Background
    void getAccountMsg() {
        bean = AppApi.getsInstance(mContext).userAccount(app.getmUserinfo().getAccess_token());
        setMsg(bean);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            if (isBind){
                isBind = false;
                mBindCard.setText("绑定银行卡");
            }else {
                isBind = true;
                getAccountMsg();
            }
        }
    }

    @UiThread
    void setMsg(UserAccountBean bean) {
        if (bean != null && !TextUtils.isEmpty(bean.getMoney())) {
            tv_money.setText(bean.getMoney());
        } else {
            baseUtil.showSnackBar(mContext, "获取账户金额失败");
        }
        if (!TextUtils.isEmpty(bean.getBank_fullname())){
            isBind = true;
            mBindCard.setText("已绑定");
        }
    }

    @Click(R.id.act_account_btn_account)
    void clickToAccount() {
        AccountDetailActivity.jumpTothisActivity(this);
    }

    @Click(R.id.act_account_btn_withdraw)
    void clickToWithdraw() {
        if (bean == null || TextUtils.isEmpty(bean.getMoney())) {
            WithdrawActivity.jumpTothisActivity(this, "***");
        } else {
            WithdrawActivity.jumpTothisActivity(this, bean.getMoney());
        }

    }

    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, UserAccountActivity_.class);
        activity.startActivity(intent);
    }

    @Override
    protected String getToolbarTitle() {
        return "我的帐户";
    }

    @Click(R.id.bind_card_item)
    void clickToBindCard() {
        BindCardActivity.jumpTothisActivity(mContext,bean,isBind);
    }
}
